using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace DataEntity
{
	public class T_Log : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlDateTime		_log_OperateTime;
			private SqlInt32		_log_OperateModuleID, _log_OperateID;
			private SqlString		_log_EmployeeID, _log_EmployeeName, _log_OperateDesc;
			private SqlGuid			_log_ID, _log_OperateModuleMainID;
		#endregion


		public T_Log()
		{
			// Nothing for now.
		}




		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Log_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidLog_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sLog_EmployeeID", SqlDbType.NVarChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_EmployeeID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sLog_EmployeeName", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_EmployeeName));
				cmdToExecute.Parameters.Add(new SqlParameter("@daLog_OperateTime", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_OperateTime));
				cmdToExecute.Parameters.Add(new SqlParameter("@iLog_OperateModuleID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _log_OperateModuleID));
				cmdToExecute.Parameters.Add(new SqlParameter("@guidLog_OperateModuleMainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_OperateModuleMainID));
				cmdToExecute.Parameters.Add(new SqlParameter("@iLog_OperateID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _log_OperateID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sLog_OperateDesc", SqlDbType.NVarChar, 1000, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_OperateDesc));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Log::Update::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Log_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidLog_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Log::Delete::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Log_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Log");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidLog_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _log_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_log_ID = (Guid)toReturn.Rows[0]["Log_ID"];
					_log_EmployeeID = (string)toReturn.Rows[0]["Log_EmployeeID"];
					_log_EmployeeName = (string)toReturn.Rows[0]["Log_EmployeeName"];
					_log_OperateTime = (DateTime)toReturn.Rows[0]["Log_OperateTime"];
					_log_OperateModuleID = (Int32)toReturn.Rows[0]["Log_OperateModuleID"];
					_log_OperateModuleMainID = (Guid)toReturn.Rows[0]["Log_OperateModuleMainID"];
					_log_OperateID = (Int32)toReturn.Rows[0]["Log_OperateID"];
					_log_OperateDesc = toReturn.Rows[0]["Log_OperateDesc"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Log_OperateDesc"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Log::SelectOne::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Log_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Log");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Log::SelectAll::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlGuid Log_ID
		{
			get
			{
				return _log_ID;
			}
			set
			{
				_log_ID = value;
			}
		}


		public SqlString Log_EmployeeID
		{
			get
			{
				return _log_EmployeeID;
			}
			set
			{
				SqlString log_EmployeeIDTmp = (SqlString)value;
				if(log_EmployeeIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_EmployeeID", "Log_EmployeeID can't be NULL");
				}
				_log_EmployeeID = value;
			}
		}


		public SqlString Log_EmployeeName
		{
			get
			{
				return _log_EmployeeName;
			}
			set
			{
				SqlString log_EmployeeNameTmp = (SqlString)value;
				if(log_EmployeeNameTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_EmployeeName", "Log_EmployeeName can't be NULL");
				}
				_log_EmployeeName = value;
			}
		}


		public SqlDateTime Log_OperateTime
		{
			get
			{
				return _log_OperateTime;
			}
			set
			{
				SqlDateTime log_OperateTimeTmp = (SqlDateTime)value;
				if(log_OperateTimeTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_OperateTime", "Log_OperateTime can't be NULL");
				}
				_log_OperateTime = value;
			}
		}


		public SqlInt32 Log_OperateModuleID
		{
			get
			{
				return _log_OperateModuleID;
			}
			set
			{
				SqlInt32 log_OperateModuleIDTmp = (SqlInt32)value;
				if(log_OperateModuleIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_OperateModuleID", "Log_OperateModuleID can't be NULL");
				}
				_log_OperateModuleID = value;
			}
		}


		public SqlGuid Log_OperateModuleMainID
		{
			get
			{
				return _log_OperateModuleMainID;
			}
			set
			{
				SqlGuid log_OperateModuleMainIDTmp = (SqlGuid)value;
				if(log_OperateModuleMainIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_OperateModuleMainID", "Log_OperateModuleMainID can't be NULL");
				}
				_log_OperateModuleMainID = value;
			}
		}


		public SqlInt32 Log_OperateID
		{
			get
			{
				return _log_OperateID;
			}
			set
			{
				SqlInt32 log_OperateIDTmp = (SqlInt32)value;
				if(log_OperateIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_OperateID", "Log_OperateID can't be NULL");
				}
				_log_OperateID = value;
			}
		}


		public SqlString Log_OperateDesc
		{
			get
			{
				return _log_OperateDesc;
			}
			set
			{
				SqlString log_OperateDescTmp = (SqlString)value;
				if(log_OperateDescTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Log_OperateDesc", "Log_OperateDesc can't be NULL");
				}
				_log_OperateDesc = value;
			}
		}
		#endregion
	}
}
